package com.sxf.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.sxf.controller.formbean.PostVO;
import com.sxf.dao.PostDAO;
import com.sxf.domain.Post;
import com.sxf.domain.PostMeta;
import com.sxf.mybatis.help.ExRowBounds;
import com.sxf.mybatis.help.QueryType;
import com.sxf.mybatis.page.Pagination;
import com.sxf.querybean.PostQuery;
@Repository
public class PostDAOImpl extends SuperDAOImpl implements PostDAO {

	private static final String STATEMENT_QUERY = "mybatis.xml.Post.query";

	@Override
	public Pagination<PostVO> queryPage(PostQuery query) {
		return queryPageTemplate(STATEMENT_QUERY, query);
	}

	@Override
	public PostVO queryOne(PostQuery query) {
		PostVO post = null;
		List<PostVO> list = st.selectList(STATEMENT_QUERY, query);
		if (list != null && !list.isEmpty()) {
			post = list.get(0);
		}
		return post;
	}

	@Override
	public List<PostVO> queryList(PostQuery query) {
		List<PostVO> postList = null;

		if (query.queryType != null
				&& PostQuery.QueryType.queryByTerm.equals(query.queryType)) {
			postList = st.selectList("mybatis.xml.Post.queryByTerm", query,
					new ExRowBounds(query.getOffset(), query.getLimit(),
							QueryType.OBJECT));
		} else {
			postList = st.selectList(STATEMENT_QUERY, query, new ExRowBounds(
					query.getOffset(), query.getLimit(), QueryType.OBJECT));
		}
		return postList;
	}

	@Override
	public long queryNumber(PostQuery query) {
		return queryNumberTemplate(STATEMENT_QUERY, query);
	}

	@Override
	public Map<String, Integer> queryStatistic() {
		String sql = "SELECT t.`status`,count(1) `count` from wp_posts t GROUP BY t.`status`";
		Map<String, Integer> map = jt.query(sql,
				new ResultSetExtractor<Map<String, Integer>>() {
					@Override
					public Map<String, Integer> extractData(ResultSet rs)
							throws SQLException, DataAccessException {
						Map<String, Integer> map = new HashMap<String, Integer>();
						while (rs.next()) {
							map.put(rs.getString(1), rs.getInt(2));
						}
						return map;
					}
				});
		return map;
	}

	public List<Date> getArchivePost() {
		return st.selectList("mybatis.xml.Post.queryArchivePostByDate");
	}

	@Override
	public int insert(Post info) {
		// sf.openSession().save(post);
		if (info != null) {
			int i = st.insert("mybatis.xml.Post.insertSelective", info);
			return i;
		}
		return 0;
	}

	@Override
	public int update(Post info) {
		// sf.openSession().update(post);
		if (info != null) {
			return st.update("mybatis.xml.Post.updateByPrimaryKeySelective",
					info);
		}
		return 0;

	}

	@Override
	public int delete(Post info) {
		// sf.openSession().delete(post);
		if (info != null) {
			return st.delete("mybatis.xml.Post.deleteByPrimaryKey", info);
		}
		return 0;
	}

	@Override
	public long insertPostMeta(PostMeta info) {
		if (info != null) {
			int i = st.insert("mybatis.xml.PostMeta.insertSelective", info);
			return i;
		}
		return 0;
	}

	@Override
	public int updatePostMeta(PostMeta info) {
		if (info != null) {
			return st.update(
					"mybatis.xml.PostMeta.updateByPrimaryKeySelective", info);
		}
		return 0;
	}

	@Override
	public int deletePostMeta(PostMeta info) {
		if (info != null && (info.getId() != null || info.getPostId() != null)) {
			return st.delete("mybatis.xml.PostMeta.deleteByPrimaryKey", info);
		}
		return 0;
	}
}
